﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FuncWorks.Xna.UI
{
	public class ButtonGuide
	{
		#region Fields / Properties
		public ButtonGuidePanelCollection Panels { get; private set; }

		float maxHeight;

		TextStyle Style;
		int Spacing;
		AnchorPoint Anchor;
		Vector2 Position;

		float currentX;
		Vector2 currentVector;
		Rectangle currentRectangle;
		#endregion

		#region Constructors
		public ButtonGuide(TextStyle textStyle, AnchorPoint anchor, int spacing, GraphicsDeviceManager graphics)
		{
			Panels = new ButtonGuidePanelCollection();
			Anchor = anchor;
			Spacing = spacing;
			Style = textStyle;

			switch (anchor)
			{
				case AnchorPoint.TopLeft:
					Position = new Vector2(graphics.GraphicsDevice.Viewport.TitleSafeArea.Left, graphics.GraphicsDevice.Viewport.TitleSafeArea.Top);
					break;

				case AnchorPoint.TopRight:
					Position = new Vector2(graphics.GraphicsDevice.Viewport.TitleSafeArea.Right, graphics.GraphicsDevice.Viewport.TitleSafeArea.Top);
					break;

				case AnchorPoint.BottomLeft:
					Position = new Vector2(graphics.GraphicsDevice.Viewport.TitleSafeArea.Left, graphics.GraphicsDevice.Viewport.TitleSafeArea.Bottom);
					break;

				case AnchorPoint.BottomRight:
					Position = new Vector2(graphics.GraphicsDevice.Viewport.TitleSafeArea.Right, graphics.GraphicsDevice.Viewport.TitleSafeArea.Bottom);
					break;

				default:
					Position = new Vector2(0, 0);
					break;
			}
		}

		public ButtonGuide(TextStyle textStyle, AnchorPoint anchor, int spacing, Vector2 position)
		{
			Panels = new ButtonGuidePanelCollection();
			Style = textStyle;
			Anchor = anchor;
			Spacing = spacing;
			Position = position;
		}
		#endregion

		public void AddPanel(string id, Texture2D icon, string text, bool visible)
		{
			Panels.Add(new ButtonGuidePanel(id, icon, text, visible));
		}

		public void Initialize()
		{
			maxHeight = 0;
			foreach (ButtonGuidePanel panel in Panels)
			{
				Vector2 textSize = Style.MeasureString(panel.Text);

				// Measure height
				float height = (panel.Icon != null ? textSize.Y > panel.Icon.Height : true) ? textSize.Y : panel.Icon.Height;
				if (height > maxHeight) { maxHeight = height; }

				// Measure width
				panel.Width = textSize.X + Spacing + (panel.Icon != null ? panel.Icon.Width : Spacing);
			}

			if (Anchor == AnchorPoint.BottomRight || Anchor == AnchorPoint.TopRight) { Panels.Reverse(); }
			if (Anchor == AnchorPoint.BottomLeft || Anchor == AnchorPoint.BottomRight) { Position.Y = Position.Y - maxHeight; }

			foreach (ButtonGuidePanel panel in Panels)
			{
				Vector2 textSize = Style.MeasureString(panel.Text);
				panel.IconY = Position.Y + ((panel.Icon != null) ? (int)Math.Ceiling((maxHeight - panel.Icon.Height) / 2.0f) : 0);
				panel.TextY = Position.Y + (int)Math.Ceiling((maxHeight - textSize.Y) / 2.0f);
			}

			currentVector = new Vector2();
			currentRectangle = new Rectangle();
		}

		public void Draw(SpriteBatch spriteBatch)
		{
			currentX = Position.X;

			foreach (ButtonGuidePanel panel in Panels)
			{
				if (!panel.Visible) { continue; }

				// Move left if anchored right; recenter vertically
				if (Anchor == AnchorPoint.BottomRight || Anchor == AnchorPoint.TopRight) { currentX -= panel.Width; }

				// Draw the icon
				if (panel.Icon != null)
				{
					currentRectangle.X = (int)currentX;
					currentRectangle.Y = (int)panel.IconY;
					currentRectangle.Width = panel.Icon.Width;
					currentRectangle.Height = panel.Icon.Height;

					spriteBatch.Draw(panel.Icon, currentRectangle, Color.White);
				}

				// Draw the text
				currentVector.X = currentX + Spacing + (panel.Icon != null ? panel.Icon.Width : Spacing);
				currentVector.Y = panel.TextY;
				Style.DrawString(spriteBatch, panel.Text, currentVector);

				// Move right if anchored left; recenter vertically
				if (Anchor == AnchorPoint.BottomLeft || Anchor == AnchorPoint.TopLeft) { currentX += panel.Width + Spacing; }
				if (Anchor == AnchorPoint.BottomRight || Anchor == AnchorPoint.TopRight) { currentX -= Spacing; }
			}
		}
	}
}
